<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/5/30
 * Time: 13:22
 * Mail:87658989@qq.com
 */

namespace app\base\service;


class Ding
{
    public $webHook;

    public function __construct($hook='')
    {
        // https://oapi.dingtalk.com/robot/send?access_token=204843c79d980cf0253f21a396e4318d15f7374c7f1d9b1d5671a27d13a81a42
        // https://oapi.dingtalk.com/robot/send?access_token=9f0df455fa73b6767b265829488671e5a696e4b4f014fe0bf2be0cf693788568
        $base = 'https://oapi.dingtalk.com/robot/send?access_token=9f0df455fa73b6767b265829488671e5a696e4b4f014fe0bf2be0cf693788568';
        $this->webHook = empty($hook) ? $base : $hook;
    }

    public function send($message='test'){
        $data        = array ('msgtype' => 'text','text' => array ('content' => $message),'at'=>array('isAtAll'=>true));
        $data_string = json_encode($data);
        $this->vpost($this->webHook,$data_string);
    }

    public function vpost($url,$data){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
}